package com.mycompany.sender;

import com.sun.mail.smtp.SMTPTransport;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender
{

   public static void Send(final String username, final String password, String recipientEmail, String title, String message) throws AddressException, MessagingException
   {
      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

      Properties props = System.getProperties();

      Session session = Session.getInstance(props, null);

      final MimeMessage msg = new MimeMessage(session);

      msg.setFrom(new InternetAddress(username + "@isd.dp.ua"));
      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

      msg.setSubject(title);
      msg.setText(message, "utf-8");
      msg.setSentDate(new Date());

      SMTPTransport t = (SMTPTransport) session.getTransport("smtp");

      t.connect("webmail.isd.dp.ua", 587, username, password);
      t.sendMessage(msg, msg.getAllRecipients());
      t.close();
   }
}
